package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;

import java.util.Calendar;

public class JiejiariActivity extends Activity {

    final int DATE_DIALOG = 1;
    int mYear, mMonth, mDay;
    private ImageButton ib_fanhui;
    private ImageButton ib_add;
    private ImageButton ib_less;
    private EditText et_amount;
    private RelativeLayout rl_time;
    private TextView tv_time;
    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//		getWindow().addFlags(
//				WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_jiejiari);
        initRes();
    }

    private void initRes() {
        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
//        ca.get(Calendar.WEEK_OF_MONTH)
        rl_time = (RelativeLayout) findViewById(R.id.rl_shijian);
        tv_time = (TextView) findViewById(R.id.tv_time);
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        ib_add = (ImageButton) findViewById(R.id.ib_add);
        ib_less = (ImageButton) findViewById(R.id.ib_less);
        et_amount = (EditText) findViewById(R.id.et_amount);
        et_amount.setText("0");
        tv_time.setText(new StringBuffer().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(" "));
        rl_time.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG);
            }
        });
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ib_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int goodsNum = Integer.parseInt(et_amount.getText().toString());
                goodsNum++;
                et_amount.setText("" + goodsNum);
            }
        });
        ib_less.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int goodsNum = Integer.parseInt(et_amount.getText().toString());
                goodsNum--;
                if (goodsNum < 0) {
                    Toast.makeText(JiejiariActivity.this, "数量不能小于0", Toast.LENGTH_SHORT).show();
                    return;
                }
                et_amount.setText("" + goodsNum);

            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    /**
     * 设置日期 利用StringBuffer追加
     */
    public void display() {
        tv_time.setText(new StringBuffer().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(" "));
    }


}
